﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace SystemWFH.Infrstructer
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
    {
        /// <summary>
        /// 上下文
        /// </summary>
        private readonly SystemWFHDbContext db;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="db">上下文服务</param>
        public BaseRepository(SystemWFHDbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> Add(T entity)
        {
            db.Set<T>().Add(entity);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> Deletes(T id)
        {
            db.Set<T>().Remove(id);
            return await db.SaveChangesAsync();

        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> Update(T entity)
        {
            db.Set<T>().Update(entity);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<T>> GetAll()
        {
            return await db.Set<T>().ToListAsync();
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> GetValue(int id)
        {
            return await db.Set<T>().FindAsync(id);
        }
        /// <summary>
        /// 带条件查询
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public async Task<List<T>> GetAsync(Expression<Func<T, bool>> predicate)
        {
            return await db.Set<T>().Where(predicate).ToListAsync();
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public async Task<int> Delete(int id)
        {
            DbSet<T> values = db.Set<T>();
            var info = await values.FindAsync(id);
            values.Remove(info);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 批量逻辑删除
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public async Task<int> GetDelete(List<T> expression)
        {
            try
            {
                db.Set<T>().UpdateRange(expression);
                return await db.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetAlls()
        {
            try
            {
                return db.Set<T>().AsQueryable();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
